# -*- coding: utf-8 -*-
"""
@Datetime: 2018/12/30
@Author: Zhang Yafei
"""


class Obj(object):
    def __init__(self, name, sex, age):
        self.name = name
        self._sex = sex
        self.__age = age

    def get_age(self):
        # print(self.__age)
        return self.__age


obj = Obj('张亚飞', '男', 23)
print(obj.name)  # 张亚飞
print(obj._sex)  # 男
# print(obj.__age)  # AttributeError: 'Obj' object has no attribute '__age'
print(obj.get_age())  # 23
print(obj._Obj__age)  # 23
"""私有成员只能类中访问，外部不能直接访问，但若要访问，可以强制访:_类名__var, 或者在内部提供一个接口供外部访问"""


class Obj2(Obj):
    def pring_sex(self):
        print(self._sex)

    def print_age(self):
        print(self.__age)


obj2 = Obj2('张亚飞', '男', 23)
obj2.pring_sex()  # 男
obj2.print_age()  # AttributeError: 'Obj2' object has no attribute '_Obj2__age'
"""私有字段只能在当前类中访问，其子类也不能访问"""